package com.baselibrary;

/**
 * @author:hanke 2023/4/18
 * desc:
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

/**
 * @Description isCanSlide 设置为false时，viewpager不能横向滑动，用在横向滑动控件与viewpager冲突时
 * @Version 1.0
 */
public class CanCtrlSlideViewPager extends ViewPager {

    boolean isCanSlide = true;

    public CanCtrlSlideViewPager(@NonNull Context context) {
        super(context);
    }

    public CanCtrlSlideViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!isCanSlide) {//设置为false时，viewpager不能横向滑动
            requestDisallowInterceptTouchEvent(true);//使viewpager不再通过onInterceptTouchEvent捕获触摸事件
        }
        return super.dispatchTouchEvent(ev);
    }

    public void setCanSlide(boolean canSlide) {
        isCanSlide = canSlide;
    }

    public boolean isCanSlide() {
        return isCanSlide;
    }
}